package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbCompanyCert;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbProductAchievementBo;
import org.dromara.system.domain.vo.WbProductAchievementVo;
import org.dromara.system.domain.WbProductAchievement;
import org.dromara.system.mapper.WbProductAchievementMapper;
import org.dromara.system.service.IWbProductAchievementService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品业绩Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-25
 */
@RequiredArgsConstructor
@Service
public class WbProductAchievementServiceImpl implements IWbProductAchievementService {

    private final WbProductAchievementMapper baseMapper;

    /**
     * 查询产品业绩
     */
    @Override
    public WbProductAchievementVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品业绩列表
     */
    @Override
    public TableDataInfo<WbProductAchievementVo> queryPageList(WbProductAchievementBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbProductAchievement> lqw = buildQueryWrapper(bo);
        Page<WbProductAchievementVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品业绩列表
     */
    @Override
    public List<WbProductAchievementVo> queryList(WbProductAchievementBo bo) {
        LambdaQueryWrapper<WbProductAchievement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbProductAchievement> buildQueryWrapper(WbProductAchievementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbProductAchievement> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, WbProductAchievement::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), WbProductAchievement::getItemName, bo.getItemName());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), WbProductAchievement::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getContractId()), WbProductAchievement::getContractId, bo.getContractId());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), WbProductAchievement::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialRemark()), WbProductAchievement::getMaterialRemark, bo.getMaterialRemark());
        lqw.eq(bo.getDataTagging() != null, WbProductAchievement::getDataTagging, bo.getDataTagging());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), WbProductAchievement::getUnit, bo.getUnit());
        lqw.like(StringUtils.isNotBlank(bo.getContractStartTime()), WbProductAchievement::getContractStartTime, bo.getContractStartTime());
        lqw.eq(bo.getActualSupplied() != null, WbProductAchievement::getActualSupplied, bo.getActualSupplied());
        lqw.orderByDesc(WbProductAchievement::getId);
        return lqw;
    }

    /**
     * 新增产品业绩
     */
    @Override
    public Boolean insertByBo(WbProductAchievementBo bo) {
        WbProductAchievement add = MapstructUtils.convert(bo, WbProductAchievement.class);
        validEntityBeforeSave(add);
        Long companyId = add.getCompanyId();
        String companyName=baseMapper.getCompanyName(companyId);
        add.setCompanyName(companyName);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品业绩
     */
    @Override
    public Boolean updateByBo(WbProductAchievementBo bo) {
        WbProductAchievement update = MapstructUtils.convert(bo, WbProductAchievement.class);
        validEntityBeforeSave(update);
        Long companyId = update.getCompanyId();
        String companyName=baseMapper.getCompanyName(companyId);
        update.setCompanyName(companyName);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbProductAchievement entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品业绩
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


}
